﻿Namespace Inventory


    Module mInventory

        Public Const cCellSize As Integer = 38
        Private Const cMaxYPos As Integer = 64
        Private Const XOffset As Integer = 18
        Private Const YOffset As Integer = 70

        Public WithEvents InventoryWindow As GUI.MirWindow

        Private bIsVisible As Boolean

        Public WithEvents mInventoryGrid As GUI.MirItemContainer
        Private WithEvents InventoryScrollBar As GUI.MirScrollBar

        'TODO: make private
        Public WithEvents HideInventory As GUI.MirButton
        'Private WithEvents HideInventory As GUI.MirButton
        
        Public SplittingStack As Int64

        Public AllowListType As ShopListType
        Public NPC_ID As Long

        'Public Weight As Integer

        Public Enum InventoryStates
            Normal = 0
            Buying = 1
            Selling = 2
            Repairing = 3
        End Enum

        Private InventoryState As InventoryStates

        Public lstAllow As List(Of ItemType)

        Private WithEvents btnFuncRepair As GUI.MirButton
        Private WithEvents btnFuncSell As GUI.MirButton

        Private intCost As Integer = -1

        Public Sub Init()

            InventoryWindow = New GUI.MirWindow("InventoryWindow", Constants.gGameResX - 282, , 282, 466, True)

            InventoryScrollBar = New GUI.MirScrollBar(InventoryWindow, "InventoryScrollBar", 260, 73, 12, 290, cMaxYPos - 7, EvoLib.Protocol.ImgLibs.GameInter, 1226)
            mInventoryGrid = New GUI.MirItemContainer(InventoryWindow, "InventoryGrid", XOffset, YOffset, 6, 8, cCellSize, cMaxYPos, DirectX.InventorySurface, DirectX.InventoryTexture, EvoLib.Protocol.ItemLocation.Inventory)

            HideInventory = New GUI.MirButton(InventoryWindow, "HideInventory", 239, 420, 0.0F, ImgLibs.GameInter, 0, 3036, 3036)

            btnFuncRepair = New GUI.MirButton(InventoryWindow, "btnFuncRepair", 196, 409, 0.0F, ImgLibs.GameInter, 353, 353, 354)
            btnFuncSell = New GUI.MirButton(InventoryWindow, "btnFuncSell", 196, 409, 0.0F, ImgLibs.GameInter, 356, 356, 357)

            btnFuncRepair.Visible = False
            btnFuncSell.Visible = False

            lstAllow = New List(Of ItemType)

        End Sub

        Public Sub Shutdown()
            InventoryWindow.Destory()
        End Sub

        Public Sub Show()

            If Not bIsVisible Then
                ToggleInventoryWindow()
            End If

        End Sub

        Public Sub Hide()

            If bIsVisible Then
                ToggleInventoryWindow()
            End If

        End Sub

        Public Sub ToggleInventoryWindow()

            If bIsVisible Then

                Windows.RemoveWindow(InventoryWindow)
                NPC_ID = 0
                btnFuncRepair.Visible = False
                btnFuncSell.Visible = False
                InventoryState = InventoryStates.Normal
                AllowListType = ShopListType.None
                mInventoryGrid.SelectItems = False
                intCost = -1

            Else

                Windows.AddWindow(InventoryWindow, Windows.WindowType.Normal)
                InventoryWindow.SetPosition(Constants.gGameResX - 302, 20)

            End If

            bIsVisible = Not bIsVisible

        End Sub

        Public Sub ServerAddItem(ByRef iteminfo As EvoLib.Protocol.ItemInfo)

            Try

                mInventoryGrid.ServerAddItem(iteminfo)

            Catch ex As Exception

                Debug.Print("Error adding item to inventory!")

            End Try

        End Sub

        Private Sub InventoryWindow_DrawWindow(ByRef mode As Globals.mGlobalVars.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles InventoryWindow.DrawWindow

            If mode = Globals.DrawingMode.Normal Then

                Libs.GameInter.Image(351).Draw(x - 114, y - 23, z)

            ElseIf mode = Globals.DrawingMode.Text Then

                Dim txt As String
                If InventoryState = InventoryStates.Repairing Or InventoryState = InventoryStates.Selling Then
                    If InventoryState = InventoryStates.Repairing Then
                        txt = "Repair Cost: "
                    ElseIf InventoryState = InventoryStates.Selling Then
                        txt = "Sell Price: "
                    End If

                    With DirectX.ProFont9
                        If intCost >= 0 Then
                            txt &= intCost.ToString & " gold"
                        Else
                            txt = "..."
                        End If
                        .DrawBorderText(txt, x + 106 - (.GetNormalTextWidth(txt) / 2), y + 429, Color.White, Color.Black, z)
                    End With
                End If

                txt = Player.Gold & " gold"
                
                With DirectX.ProFont9
                    .DrawBorderText(txt, x + 106 - (.GetNormalTextWidth(txt) / 2), y + 409, Color.White, Color.Black, z)
                    txt = Player.PlayerWeight.INT4_BAGWEIGHT & " / " & Player.Stats.INT2_MAX_WEIGHT & " Bag Weight"
                    .DrawBorderText(txt, x + 144 - (.GetNormalTextWidth(txt) / 2), y + 25, Color.White, Color.Black, z)
                End With




            End If

        End Sub

        Private Sub InventoryScrollBar_Changed() Handles InventoryScrollBar.Changed
            mInventoryGrid.mYScroll = InventoryScrollBar.Value
        End Sub

        Private Sub InventoryWindow_InterfaceMouseEvent(ByRef type As Mouse.mMouseVars.MouseInputType, ByRef e As System.Windows.Forms.MouseEventArgs, ByRef InterfaceItem As Windows.mWindows.GUIObj) Handles InventoryWindow.InterfaceMouseEvent
            'Game.PlayerInputMouseCheck(type, e)
        End Sub

        Private Sub InventoryWindow_MouseEvent(ByRef type As Mouse.mMouseVars.MouseInputType, ByRef e As System.Windows.Forms.MouseEventArgs) Handles InventoryWindow.MouseEvent
            'Game.PlayerInputMouseCheck(type, e)
            GameWindow.HideTooltip()
        End Sub

        Private Sub mInventoryGrid_AddedItem(ByRef item As EvoLib.Protocol.ItemInfo, skipupdate As Boolean) Handles mInventoryGrid.AddedItem

            If Not skipupdate Then

                Dim Packet As EvoLib.Protocol.Packet
                Packet.Username = Globals.sUsername
                Packet.PacketType = EvoLib.Protocol.GamePacketType.ChangeItemLocation
                Packet.PacketOpCode = EvoLib.Protocol.PacketOpCode.Notify

                Dim struct As EvoLib.Protocol.ItemLocationInfo
                struct.INT_ID = item.INT_ID
                struct.location = EvoLib.Protocol.ItemLocation.Inventory

                ClientSock.SendData(EvoLib.Protocol.MakePacket(Packet, struct, Globals.Crypto))

            End If

        End Sub

        Public Sub Reset()

            mInventoryGrid.Destroy()
            mInventoryGrid = New GUI.MirItemContainer(InventoryWindow, "InventoryGrid", XOffset, YOffset, 6, 8, cCellSize, cMaxYPos, DirectX.InventorySurface, DirectX.InventoryTexture, EvoLib.Protocol.ItemLocation.Inventory)

        End Sub

        Private Sub mInventoryGrid_OnSelectedItemChange() Handles mInventoryGrid.OnSelectedItemChange

            If Inventory.InventoryState = InventoryStates.Selling Or Inventory.InventoryState = InventoryStates.Repairing Then

                If mInventoryGrid.SelectedItem.ItemDesc.info.INT_ID <> 0 And lstAllow.Contains(mInventoryGrid.SelectedItem.ItemDesc.info.Base.BYTE_TYPE) Then

                    Dim pkt As Packet

                    pkt.PacketType = GamePacketType.NPCShopSell
                    pkt.PacketOpCode = PacketOpCode.Enquiry
                    pkt.Username = Globals.sUsername

                    Dim struct As ShopItemSellInfo

                    struct.ID = mInventoryGrid.SelectedItem.ItemDesc.info.INT_ID
                    struct.ID_NPC = NPC_ID
                    If Inventory.InventoryState = InventoryStates.Selling Then
                        struct.BYTE_DEFTYPE = ShopSellType.SellGetPrice
                    Else
                        struct.BYTE_DEFTYPE = ShopSellType.RepairGetCost
                    End If
                    struct.INT_COUNT = 1

                    ClientSock.SendData(MakePacket(pkt, struct, Globals.Crypto))

                    intCost = -1

                Else

                    intCost = -1

                End If

            End If

        End Sub

        Private Sub mInventoryGrid_RefreshingSurfaces(ByRef surf As Microsoft.DirectX.Direct3D.Surface, ByRef tex As Microsoft.DirectX.Direct3D.Texture) Handles mInventoryGrid.RefreshingSurfaces

            surf = DirectX.InventorySurface
            tex = DirectX.InventoryTexture

        End Sub

        Private Sub HideInventory_Click() Handles HideInventory.Click
            ToggleInventoryWindow()
        End Sub

        Public Sub ClearAllowList()

            lstAllow.Clear()

        End Sub

        Public Sub EndAllowList()

            If Not bIsVisible Then

                ToggleInventoryWindow()

            End If

            Select Case AllowListType

                Case ShopListType.Sell
                    btnFuncSell.Visible = True
                    btnFuncRepair.Visible = False
                    InventoryState = InventoryStates.Selling
                    mInventoryGrid.SelectItems = True

                Case ShopListType.Repair
                    btnFuncSell.Visible = False
                    btnFuncRepair.Visible = True
                    InventoryState = InventoryStates.Repairing
                    mInventoryGrid.SelectItems = True

                Case ShopListType.None
                    btnFuncSell.Visible = False
                    btnFuncRepair.Visible = False
                    InventoryState = InventoryStates.Normal
                    mInventoryGrid.SelectItems = False

            End Select

        End Sub

        Public Sub AddAllow(item_type As ItemType)

            lstAllow.Add(item_type)

        End Sub

        Public Sub SetState(istate As InventoryStates)

            InventoryState = istate

        End Sub

        Private Sub btnFuncSell_Click() Handles btnFuncSell.Click

            If mInventoryGrid.SelectedItem.ItemDesc.info.INT_ID = 0 Then Exit Sub

            If mInventoryGrid.SelectedItem.ItemDesc.info.INT_COUNT > 1 Then

                MessageBoxes.AddMessageBox("SELLSTACK", "How many do you want to sell?", MessageBoxes.MsgBoxButtons.Input, AddressOf MsgBoxResultCallback, 3, , "0123456789")

            Else

                Dim pkt As Packet

                pkt.PacketType = GamePacketType.NPCShopSell
                pkt.PacketOpCode = PacketOpCode.Enquiry
                pkt.Username = Globals.sUsername

                Dim struct As ShopItemSellInfo

                struct.ID = mInventoryGrid.SelectedItem.ItemDesc.info.INT_ID
                struct.ID_NPC = NPC_ID
                struct.BYTE_DEFTYPE = ShopSellType.SellDoIt
                struct.INT_COUNT = 1

                ClientSock.SendData(MakePacket(pkt, struct, Globals.Crypto))

                mInventoryGrid.SelectItems = False
                mInventoryGrid.SelectItems = True

            End If

        End Sub

        Private Sub btnFuncRepair_Click() Handles btnFuncRepair.Click

            If mInventoryGrid.SelectedItem.ItemDesc.info.INT_ID = 0 Then Exit Sub

            Dim pkt As Packet
            pkt.PacketType = GamePacketType.NPCShopSell
            pkt.PacketOpCode = PacketOpCode.Enquiry
            pkt.Username = Globals.sUsername

            Dim struct As ShopItemSellInfo
            struct.ID = mInventoryGrid.SelectedItem.ItemDesc.info.INT_ID
            struct.ID_NPC = NPC_ID
            struct.BYTE_DEFTYPE = ShopSellType.RepairDoIt
            ClientSock.SendData(MakePacket(pkt, struct, Globals.Crypto))

            mInventoryGrid.SelectItems = False
            mInventoryGrid.SelectItems = True

        End Sub


        Public Sub MsgBoxResultCallback(ByRef result As MessageBoxes.MsgBoxResult)

            Select Case result.mName

                Case "SELLSTACK"

                    Dim pkt As Packet

                    pkt.PacketType = GamePacketType.NPCShopSell
                    pkt.PacketOpCode = PacketOpCode.Enquiry
                    pkt.Username = Globals.sUsername

                    Dim struct As ShopItemSellInfo

                    struct.ID = mInventoryGrid.SelectedItem.ItemDesc.info.INT_ID
                    struct.ID_NPC = NPC_ID
                    struct.BYTE_DEFTYPE = ShopSellType.SellDoIt
                    If result.mInput.Length = 0 Then
                        struct.INT_COUNT = mInventoryGrid.SelectedItem.ItemDesc.info.INT_COUNT
                    Else
                        struct.INT_COUNT = CInt(result.mInput)
                    End If
                    If mInventoryGrid.SelectedItem.ItemDesc.info.INT_COUNT < struct.INT_COUNT Then
                        struct.INT_COUNT = mInventoryGrid.SelectedItem.ItemDesc.info.INT_COUNT
                    End If
                    ClientSock.SendData(MakePacket(pkt, struct, Globals.Crypto))

                    mInventoryGrid.SelectItems = False
                    mInventoryGrid.SelectItems = True

            End Select

        End Sub

        Public Sub SellInfo(info As NPCShopSellPrice)

            If mInventoryGrid.SelectedItem.ItemDesc.info.INT_ID = info.ID Then
                intCost = info.INT8_OFFER
            Else
                intCost = -1
            End If

        End Sub

    End Module

End Namespace
